﻿using MQTTnet.Server;
using System;
using System.Collections.Generic;
using System.Text;
using adb;
using Microsoft.EntityFrameworkCore;
using Masuit.Tools;
using System.Threading.Tasks;

namespace SensorCollection
{
    /// <summary>
    /// 机组状态
    /// </summary>
    public class SignalStateData : IDataDealwith
    {
        private MqttServer mqttServer;
        private DbContextOptionsBuilder<DeviceDbContext> optionsBuilder;

        public DbContextOptionsBuilder<MyDbContext> MyOptionsBuilder { get; }

        public SignalStateData(MqttServer mqttServer, DbContextOptionsBuilder<DeviceDbContext> optionsBuilde, DbContextOptionsBuilder<MyDbContext> MyOptionsBuilder)
        {
            this.mqttServer = mqttServer;
            this.optionsBuilder = optionsBuilde;
            this.MyOptionsBuilder = MyOptionsBuilder;
        }

        public long keyId => 820090;


        public object JsonToModel(string json, string deviceSN = null)
        {
            var data = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, decimal>>(json);
            if(data == null)
            {
                return System.Text.Json.JsonSerializer.Deserialize<DtuDataFormat<Dictionary<string, decimal>>>(json);
            }
            return data;
            //if ( ("wsr".Equals(deviceSN)) || "YKF2".Equals(deviceSN) || "YKF1".Equals(deviceSN))
            //{
            //    return System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, decimal>>(json);
            //}
            //else
            //{
            //    return System.Text.Json.JsonSerializer.Deserialize<DtuDataFormat<Dictionary<string, decimal>>>(json);

            //}
        }
        public async Task ReactData(object obj, string Topic, string deviceSN = null)
        {
            try
            {
                Dictionary<string, decimal> obj1 = obj as Dictionary<string, decimal>;
                if (obj1 == null) return;
                if (Topic.IsNullOrEmpty()) return;

                if ("wsr".Equals(deviceSN))
                {
                    tSignalStateData_WSR ssd = new tSignalStateData_WSR();
                    ssd.Topic = Topic;
                    ssd.addTime = DateTime.Now;
                    Type t = typeof(tSignalStateData_WSR);
                    foreach (var item in obj1)
                    {
                        t.GetProperty(item.Key)?.SetValue(ssd, item.Value);
                    }
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSignalStateData_WSR.AddAsync(ssd);
                        await db.SaveChangesAsync();
                    }
                }
                else if ("YKF2".Equals(deviceSN))
                {
                    tSignalStateData_YKF2 ssd2ykf2 = new tSignalStateData_YKF2();
                    ssd2ykf2.Topic = Topic;
                    ssd2ykf2.addTime = DateTime.Now;
                    Type type = typeof(tSignalStateData_YKF2);
                    foreach (var item in obj1)
                    {
                        type.GetProperty(item.Key)?.SetValue(ssd2ykf2, item.Value);
                    }
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSignalStateData_YKF2.AddAsync(ssd2ykf2);
                        await db.SaveChangesAsync();
                    }
                }
                else if ("YKF1".Equals(deviceSN))
                {
                    tSignalStateData_YKF1 ssd2ykf2 = new tSignalStateData_YKF1();
                    ssd2ykf2.Topic = Topic;
                    ssd2ykf2.addTime = DateTime.Now;
                    Type type = typeof(tSignalStateData_YKF1);
                    foreach (var item in obj1)
                    {
                        type.GetProperty(item.Key)?.SetValue(ssd2ykf2, item.Value);
                    }
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSignalStateData_YKF1.AddAsync(ssd2ykf2);
                        await db.SaveChangesAsync();
                    }
                }
                else if ("XAFD".Equals(deviceSN))
                {
                    tSignalStateData_XAFD ssd = new tSignalStateData_XAFD();
                    ssd.Topic = Topic;
                    ssd.addTime = DateTime.Now;
                    Type type = typeof(tSignalStateData_XAFD);
                    foreach (var item in obj1)
                    {
                        type.GetProperty(item.Key)?.SetValue(ssd, item.Value);
                    }
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSignalStateData_XAFD .AddAsync(ssd);
                        await db.SaveChangesAsync();
                    }
                }
                else
                {
                    tSignalStateData ssd = new tSignalStateData();
                    ssd.Topic = Topic;
                    ssd.addTime = DateTime.Now;
                    Type t = typeof(tSignalStateData);
                    foreach (var item in obj1)
                    {
                        t.GetProperty(item.Key)?.SetValue(ssd, item.Value);
                    }
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tSignalStateData.AddAsync(ssd);
                        await db.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine("tSignalStateData:" + ex.Message);
            }
        }

    }
}
